<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证用户名</title>
    <style>
        .right {
            color: green;
        }

        .error {
            color: red;
        }
    </style>
</head>

<body>
    <input type="text" name="" id="">
    <span></span>

    <script>
        const input = document.querySelector('input')
        const span = document.querySelector('span')
        const regex = /^[a-zA-Z0-9-_]{6,16}$/
        input.addEventListener('blur', () => {
            if (regex.test(this.value)) {
                span.className = 'right'
                span.innerHTML = '输入正确'
            }else{
                span.className = 'error'
                span.innerHTML = '格式错误'
            }
        })
    </script>
</body>

</html>